package com.ubi.core.json;

import com.ubi.core.utils.converter.DateConverter;
import org.apache.commons.lang3.time.DateFormatUtils;

import java.text.DateFormat;
import java.text.FieldPosition;
import java.text.ParsePosition;
import java.util.Date;

public class SiteDateFormat extends DateFormat {

    private static final long serialVersionUID = 2L;

    private final transient DateConverter dateConverter = new DateConverter();

    @Override
    public StringBuffer format(Date date, StringBuffer toAppendTo, FieldPosition fieldPosition) {
        toAppendTo.append(DateFormatUtils.format(date, DateConverter.DATE_FORMAT_WITHOUT_HOURS));
        return toAppendTo;
    }

    @Override
    public Date parse(String source, ParsePosition pos) {
        pos.setIndex(source.length());
        return dateConverter.convert(source);
    }

    @Override
    public Object clone() {
        return this;
    }
}
